package com.ruoyi.web.controller.system;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SubscribeDate;
import com.ruoyi.system.domain.tumor.ReportRecord;
import com.ruoyi.system.service.ISubscribeDateService;
import com.ruoyi.system.service.tumor.IReportRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 可预约时间设置Controller
 *
 * @author ruoyi
 * @date 2023-08-14
 */
@RestController
@RequestMapping("/system/subscribedate")
public class SubscribeDateController extends BaseController {
    @Autowired
    private ISubscribeDateService subscribeDateService;

    @Autowired
    private IReportRecordService iReportRecordService;


    /**
     * 查询可预约时间设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:subscribedate:list')")
    @GetMapping("/list")
    public TableDataInfo list(SubscribeDate subscribeDate) {
        //        startPage();
        List<SubscribeDate> list = subscribeDateService.selectSubscribeDateList(subscribeDate);
        return getDataTable(list);
    }

    /**
     * 导出可预约时间设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:subscribedate:export')")
    @Log(title = "可预约时间设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SubscribeDate subscribeDate) {
        List<SubscribeDate> list = subscribeDateService.selectSubscribeDateList(subscribeDate);
        ExcelUtil<SubscribeDate> util = new ExcelUtil<SubscribeDate>(SubscribeDate.class);
        util.exportExcel(response, list, "可预约时间设置数据");
    }

    /**
     * 获取可预约时间设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:subscribedate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(subscribeDateService.selectSubscribeDateById(id));
    }

    /**
     * 新增可预约时间设置
     */
    @PreAuthorize("@ss.hasPermi('system:subscribedate:add')")
    @Log(title = "可预约时间设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SubscribeDate subscribeDate) {
        //拼接字段 subscribeDate

        if(subscribeDate.getYear() != null&& subscribeDate.getMonth() != null && subscribeDate.getDay() != null){
            String month = subscribeDate.getMonth()> 9?subscribeDate.getMonth()+"":"0"+subscribeDate.getMonth();
            String day = subscribeDate.getDay()> 9?subscribeDate.getDay()+"":"0"+subscribeDate.getDay();
            String subscribeDateStr = subscribeDate.getYear() +"-" + month + "-" + day;
            subscribeDate.setSubscribeDate(subscribeDateStr);
        }
        return toAjax(subscribeDateService.insertSubscribeDate(subscribeDate));
    }

    /**
     * 修改可预约时间设置
     */
    @PreAuthorize("@ss.hasPermi('system:subscribedate:edit')")
    @Log(title = "可预约时间设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SubscribeDate subscribeDate) {
        return toAjax(subscribeDateService.updateSubscribeDate(subscribeDate));
    }

    /**
     * 删除可预约时间设置
     */
    @PreAuthorize("@ss.hasPermi('system:subscribedate:remove')")
    @Log(title = "可预约时间设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{year}/{month}/{day}")
    public AjaxResult remove(@PathVariable Integer year, @PathVariable Integer month, @PathVariable Integer day) {

        //先根据年月日找到对应的id，并且通过id找到record表中是否已经存在数据，如果不存在，则可以删除，否则，不可以擅长
        SubscribeDate subscribeDate = subscribeDateService.getSubscribeDate(year,month,day);
        if(subscribeDate != null){
            Long id = subscribeDate.getId();

            ReportRecord record = new ReportRecord();
            record.setDelFlag("0");
            record.setSubscribeDateId(id);
            record.setSubscribeStatus("1");
            List<ReportRecord> reportRecordList = iReportRecordService.selectReportRecordList(record);
            if(CollectionUtil.isNotEmpty(reportRecordList)){
                return AjaxResult.error("当前日期已经存在预约记录，不能删除");
            }
        }
        return toAjax(subscribeDateService.deleteSubscribeDate(year, month, day));
    }
}
